/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package table;

import entities.BookTables;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.BookTablesFacadeLocal;
import session.ResTableFacadeLocal;
import session.RestaurantsFacadeLocal;

/**
 *
 * @author TANGQUOCTUAN
 */
public class ControllerTables extends HttpServlet {

    @EJB
    private RestaurantsFacadeLocal restaurantsFacade;
    @EJB
    private ResTableFacadeLocal resTableFacade;
    @EJB
    private BookTablesFacadeLocal bookTablesFacade;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            HttpSession session = request.getSession();
            String action = request.getParameter("action");
            if (action.equals("Table")) {
                ServletContext context = getServletContext();
                List table = this.resTableFacade.showTable();
                context.setAttribute("listTable", table);
                System.out.println("afsdasdad" + table);
                ServletContext contextloadres = getServletContext();
                List id = this.restaurantsFacade.getResName();
                contextloadres.setAttribute("resid", id);
                request.getRequestDispatcher("Tables.jsp").forward(request, response);
            } else if (action.equals("AdminTables")) {
                ServletContext context = getServletContext();
                ServletContext contextloadres = getServletContext();
                List table = this.resTableFacade.showTable();
                context.setAttribute("listAdminTables", table);
                List id = this.restaurantsFacade.getResName();
                contextloadres.setAttribute("resid", id);
                request.getRequestDispatcher("AdminTables.jsp").forward(request, response);
            } else if (action.equals("Update")) {
                Integer id = new Integer(request.getParameter("id"));
                ServletContext context = getServletContext();
                List table = this.resTableFacade.detailTables(id);
                context.setAttribute("updateTable", table);
                System.out.println("afsdasdad" + table);
                request.getRequestDispatcher("UpdateTables.jsp").forward(request, response);
            } else if (action.equals("AdminBookTables")) {
                //ServletContext context = getServletContext();
                List<BookTables> booktable = this.bookTablesFacade.findAll();
                request.setAttribute("listadminbooktable", booktable);
                request.getRequestDispatcher("AdminBookTables.jsp").forward(request, response);
            } else if (action.equals("Insert")) {
                Integer seat = new Integer(request.getParameter("txtseat"));
                Integer id = new Integer(request.getParameter("txtid"));
                String des = request.getParameter("txtdes");
                this.resTableFacade.insertTable(id, seat, des, "");
                response.sendRedirect("ControllerTables?action=AdminTables");
            } else if (action.equals("Delete")) {
                Integer id = new Integer(request.getParameter("id"));
                this.resTableFacade.deleteTable(id);
                response.sendRedirect("ControllerTables?action=AdminTables");
            } else if (action.equals("BookTable")) {
                ServletContext contextloadtable = getServletContext();
                ServletContext contextloadres = getServletContext();
                List tablename = this.resTableFacade.getTableName();
                contextloadtable.setAttribute("name", tablename);
                List id = this.restaurantsFacade.getResName();
                contextloadres.setAttribute("resid", id);
                request.getRequestDispatcher("BookTables.jsp").forward(request, response);
            } else if (action.equals("Book")) {
                Integer seatid = new Integer(request.getParameter("txtseat"));
                Integer res = new Integer(request.getParameter("txtres"));
                String custid = request.getParameter("txtcustid");
                Date oder = new Date(request.getParameter("txtoder"));
                BigDecimal fee = new BigDecimal(request.getParameter("txtreserve"));
                this.bookTablesFacade.insertbooktable(seatid, res, custid, oder, fee);
                response.sendRedirect("index.jsp");
            } else if (action.equals("Updates")) {

                List<BookTables> list = bookTablesFacade.findAll();
                for (int i = 0; i < list.size(); i++) {
                    BookTables accfind = bookTablesFacade.find(list.get(i).getBookTableID());
                    accfind.setStatus(false);
                    bookTablesFacade.edit(accfind);

                }
                String[] id = request.getParameterValues("chkStatus");
                if (id != null) {
                    for (int i = 0; i < id.length; i++) {
                        BookTables book = bookTablesFacade.find(Integer.parseInt(id[i]));
                        book.setStatus(true);
                        bookTablesFacade.edit(book);
                    }
                    response.sendRedirect("ControllerTables?action=AdminBookTables");
                } else {
                    response.sendRedirect("ControllerTables?action=AdminBookTables");
                }

            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
